package nrd;

import static org.junit.Assert.assertTrue;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.RoundStartedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * Verify that move1 from TheOrangeRobot works properly in all cases. Test against SittingDuck to
 * check the move1 calls and executes properly.
 * 
 * @author Nathan Dorman
 * 
 */
public class TestTheOrangeRobotMove1 extends RobotTestBed {

  double xStart = 0;
  double yStart = 0;

  boolean completesMove1 = false;

  /**
   * Specifies that SittingDuck and TheOrangeRobot are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,nrd.TheOrangeRobot";
  }

  /**
   * This test runs for 10 round.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 10;
  }

  /**
   * TheOrangeRobot move1 will move ahead 75 pixels. Pythagoreans theorem is used to calculate the
   * traversed distance. An error margin of 2.5 pixels is allowed. Test fails if this robot only
   * moves ahead into walls every time.
   * 
   * @param event Info about the current state of the battle.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    double xPos = robot.getX();
    double yPos = robot.getY();
    double distance = -10;
    // calculate distance for movement into any quadrant
    if (xPos >= xStart) {
      if (yPos >= yStart) {
        distance = Math.sqrt((Math.pow((xPos - xStart), 2)) + (Math.pow((yPos - yStart), 2)));
      }
      else {
        distance = Math.sqrt((Math.pow((xPos - xStart), 2)) + (Math.pow((yStart - yPos), 2)));
      }

    }
    else {
      if (yPos >= yStart) {
        distance = Math.sqrt((Math.pow((xStart - xPos), 2)) + (Math.pow((yPos - yStart), 2)));
      }
      else {
        distance = Math.sqrt((Math.pow((xStart - xPos), 2)) + (Math.pow((yStart - yPos), 2)));
      }
    }
    // error margin of 2.5 pixels
    if (Math.abs(75 - distance) < 2.5) {
      completesMove1 = true;
    }

  }

  /**
   * Gets the start position of the robot for distance calculations.
   * 
   * @param event details of the opening round
   */
  @Override
  public void onRoundStarted(RoundStartedEvent event) {
    IRobotSnapshot robot = event.getStartSnapshot().getRobots()[1];
    xStart = robot.getX();
    yStart = robot.getY();
  }

  /**
   * Tells the results of the battle.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertTrue("Robot Completes move1.", completesMove1);
  }
}
